package com.zzty.taskapp.daoImpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zzty.taskapp.common.DBUtil;
import com.zzty.taskapp.dao.ClassDAO;
import com.zzty.taskapp.entity.Clazz;

public class ClassDAOImpl implements ClassDAO {

	public int searchClassidByName(String name) {
		String sql = "select id from class where name=?";
		PreparedStatement ps = DBUtil.getPreparedStatement(sql);
		int classid = 0;
		try {
			ps.setString(1, name);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				classid = rs.getInt("id");

			}
			DBUtil.closeConnection();
			return classid;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return classid;

	}

	public int searchClassidByOwner(String name) {
		String sql = "select id from class where Owner=?";
		PreparedStatement ps = DBUtil.getPreparedStatement(sql);
		int classid = 0;
		try {
			ps.setString(1, name);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				classid = rs.getInt("id");

			}
			DBUtil.closeConnection();
			return classid;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return classid;

	}

	public String searchClassNameByOwner(String name) {
		String sql = "select Name from class where Owner=?";
		PreparedStatement ps = DBUtil.getPreparedStatement(sql);
		String className = "";
		try {
			ps.setString(1, name);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				className = rs.getString("Name");

			}
			DBUtil.closeConnection();
			return className;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return className;
	}

	public List<Clazz> getAllClazz() {
		List<Clazz> clazzs=new ArrayList<Clazz>();
		String sql = "select * from class ";
		PreparedStatement ps = DBUtil.getPreparedStatement(sql);
		try {
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Clazz clazz=new Clazz();
				clazz.setId(rs.getInt("id"));
				clazz.setName(rs.getString("name"));
				clazzs.add(clazz);
			}
			DBUtil.closeConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clazzs;
	}

}
